from django.shortcuts import render
from django.views.generic.base import View
# Create your views here.
import string,random
from captcha.image import ImageCaptcha
import django_redis
from django.http import HttpResponse,JsonResponse
import logging

# from mgproject.utils.huyi_sms.sms3 import send_sms_code
from mgproject.utils.huyi_sms.sms3 import send_sms_code

logger = logging.getLogger('django')

# 图片验证码
class ImgcodeView(View):
    def get(self, request,uuid):
        """
        # 图片验证码资源
        :param request
        :return:
        """
        # 随即生成验证码（四位数值类型）
        seed = string.digits  # 他会生成 0123456789
        "k=几就随即生成几位"
        r = random.choices(seed, k=4)  # ['1','7','6','2']
        imgcode_str = ''.join(r)  # 1762

        #  根据1生成的数字，创建图片验证码资源
        imgcode = ImageCaptcha().generate(chars=imgcode_str)

        # 将imgcode_str存入redis数据库中去
        redis_conn = django_redis.get_redis_connection('verify_code')
        redis_conn.setex('img_%s'%uuid,300,imgcode_str)

        # 返回资源给前端
        return HttpResponse(imgcode,content_type='image/png')


# 短信验证码
class SMScodeView(View):
    def get(self, request, phone):
        """
        匹配并删除图形验证码
        :param request
        ：param phone:
        :return:
        """
        # 1.获取请求参数（路径参数+查询字符串）
        imgcode_client = request.GET.get('imgcode','')
        uuid = request.GET.get('uuid','')

        # 2.校验参数(完整性)
        if not all([phone,imgcode_client,uuid]):
            return JsonResponse({'code':'4001','errormsg':'缺少必传参数'})

        # 建立redis数据库的连接
        redis_conn = django_redis.get_redis_connection('verify_code')
        is_send = redis_conn.get('is_send_%s'%phone)

        # 获取is_send并iqu判断是否过于频繁
        if is_send:
            return JsonResponse({'code':'4004','errormsg':'发送短信验证码过于频繁'})

        # 3.获取服务器生成的验证码

        imgcode_server = redis_conn.get('img_%s'%uuid)

        # 4.匹配（非空判断/一致性判断）
        if imgcode_server is None:
            return JsonResponse({'code':'4002','errormsg':'图片验证码失效'})

        imgcode_server = imgcode_server.decode('utf-8')
        print(imgcode_server)

        if imgcode_client.lower() != imgcode_server.lower():
            return JsonResponse({'code':'4003','errormsg':'图片验证码不一致'})

        # 5.删除图片验证码（避免用户恶意测试）
        try:
            redis_conn.delete('img_%s'%uuid)
        except Exception as e:
            logger.error(e)

        # 6.生成短信验证码(6位)
        seed = string.digits
        r = random.choices(seed,k=6)
        smscode_str = "".join(r)

        print('smscode:',smscode_str)

        # 7.保存短信验证码到redis数据库中
        # redis_conn.setex('sms_%s'%phone,60,smscode_str)
        # redis_conn.setex('is_send_%s'%phone,60,1)

        # 获取管道对象
        pl = redis_conn.pipeline()
        pl.setex('sms_%s'%phone,60,smscode_str)
        pl.setex('is_send_%s'%phone,60,1)
        # 同时执行
        pl.execute()

        # 异步执行短信验证码功能
        # from mgproject.celery_tasks.sms.tasks import huyi_send_sms_code
        #
        # # Celery异步发送短信验证码
        # ret = huyi_send_sms_code.delay(phone, smscode_str)
        #
        # # 8. 根据外部接⼝返回值响应前端结果
        # if ret:  # 执⾏⼀个任务就返回⼀个taskid 689e889c-a607-49f3-9777-248a8dcce310
        #     return JsonResponse({'code': '200', 'errormsg': 'OK'})
        #
        # return JsonResponse({'code': '5001', 'errormsg': '发送短信验证码错误'})



        # 8.发送短信验证码
        # ret = send_sms_code(smscode_str,phone)

        # 9.根据外部接口返回值响应前端结果
        ret = {'code':2}
        # if ret.code == 2:
        if ret.get('code') == 2:
            # print(12131)
            return JsonResponse({'code':200,'errormsg':'OK'})
        return JsonResponse({'code':'5001','errormsg':'发送短信验证码错误！'})


class ChecKSMScode(View):
    def get(self,request,phone):
        """
        用户注册时的短信验证码检验
        """
        # 1.接受请求参数
        smscode_client = request.GET.get('smscode','')

        # 2.校验参数
        if not all([phone,smscode_client]):
            return JsonResponse({'code':'4001','errormsg':'缺少比传参数'})

        # 查询服务器端的短信验证码
        redis_conn = django_redis.get_redis_connection('verify_code')
        smscode_server = redis_conn.get('sms_%s'%phone)

        # 3.匹配(非空判断/有效判断)
        if smscode_server is None:
            return JsonResponse({'code':'4002','errormsg':'短信验证码失效'})

        smscode_server = smscode_server.decode('utf-8')

        if smscode_client != smscode_server:
            return JsonResponse({'code':'4003','errormsg':'短信验证码不一致'})

        # 4.响应结果
        return JsonResponse({'code':'200','errormsg':'ok'})






